/**
 * @author 黄波
 * 创建日期：2011Oct 16, 2011
 * TODO// N/A
 */
package com.platform.res.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.pojo.TmpRes;
import com.platform.pojo.UserInfo;
import com.platform.res.bo.ResBo;

/**
 * 临时资源下载
 */
public class TmpResDownloadAction extends ResourceSupport {

	private String id;	//申请下载的资源ID

	@Override
	public String execute() throws Exception {
		//判断当前用户有没有登录
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserInfo user = (UserInfo) session.getAttribute("user");
		if(user == null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String reqURI = "njcc/loadResItem.action"+ "?id=" + id;
//			String reqURL = request.getRequestURL().toString() + "?id=" + id;
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('请在登录后下载！');window.location.href=\""+ ServletActionContext.getServletContext().getContextPath() + "/" +"login.jsp?refreshurl=" + reqURI + "\";</script>");
			out.close();
			return null;
		}

		if(user.getIsAudit().intValue() != 1) {
			//用户未登录
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('帐户锁定中，不能下载资源，请联系管理员解锁！');history.back();;</script>");
			out.close();
			return null;
		}
		if(id == null || id.equals("")) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('资源信息为空！');history.back();</script>");
			out.close();
			return null;
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		ResBo resBo = ResBo.getInstance();
		TmpRes info = resBo.getTmpRes(id);
		String path = ServletActionContext.getServletContext().getRealPath(info.getUrl()); 
        
        File fileToDown = new File(path); 
        if(!fileToDown.exists()) {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('资源不存在或已被删除！');history.back();</script>");
			out.close();
			return null;
        }
        ServletOutputStream out = response.getOutputStream(); 
        FileInputStream fileIn = new FileInputStream(fileToDown); 
        BufferedInputStream buffer = new BufferedInputStream(fileIn); 
        int fileLength = fileIn.available(); 
        byte[] fileBytes = new byte[1024]; 
        //设置文件大小 
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentLength(fileLength); 
        response.setContentType("application/x-msdownload");   
        response.addHeader("Content-Disposition","attachment; filename=\"" + URLEncoder.encode(info.getName(), "utf-8") + "\""); 
        //输出文件流 
        while   (true)   { 
                int   isOver   =   buffer.read(fileBytes); 
                out.write(fileBytes); 
                if (isOver   ==   -1) {
                     break; 
                }
        } 
        buffer.close();
        out.close(); 
        return null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
